﻿/***********************************************************************
 * <copyright file="CommonFunction.cs" company="Linh Khang">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Author:   LinhMC
 * Email:    linhmc.vn@gmail.com
 * Website:
 * Create Date: Sunday, February 09, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace WindowsForms.AOnline.WindowsForm.CommonClass
{
    public class CommonFunction
    {

        public static XtraUserControl CommonUserControl = null;
        /// <summary>
        /// Using to add user control to panel
        /// </summary>
        /// <param name="oContainerPanel">Panel of Mainform contain UserControl </param>
        /// <param name="oXtraUserControl">UserControl to add Panel</param>
        /// <param name="oType">Type of UserControl</param>
        public static void AddCotrolToPanel(XtraPanel oContainerPanel, XtraUserControl oXtraUserControl, Type oType)
        {
            try
            {
                if (oContainerPanel.Controls.Count > 0)
                {
                    var no = false;
                    // ReSharper disable once UnusedVariable
                    foreach (var control in oContainerPanel.Controls.Cast<object>().Where(control => control.GetType() == oType))
                    {
                        no = true;
                    }
                    if (no == false)
                    {
                        oContainerPanel.Controls.Clear();
                        oContainerPanel.Controls.Add(oXtraUserControl);
                    }
                }
                else
                {
                    oContainerPanel.Controls.Add(oXtraUserControl);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// Run User Control
        /// </summary>
        /// <param name="userControl"></param>
        /// <param name="oContainerPanel"></param>
        public static void RunUserControl(XtraUserControl userControl, XtraPanel oContainerPanel)
        {
            if (CommonUserControl == null)
            {
                CommonUserControl = userControl;
                AddCotrolToPanel(oContainerPanel, CommonUserControl, typeof(XtraUserControl));
            }
            else
            {
                if (CommonUserControl.GetType() != userControl.GetType())
                {
                    CommonUserControl = userControl;
                    AddCotrolToPanel(oContainerPanel, CommonUserControl, typeof(XtraUserControl));
                }
            }
        }
    }
}
